<?php
namespace app\web\admin;

use app\common\builder\ZBuilder;
use app\web\model\Order as myModel;
use app\web\model\Technician;
use app\web\model\TechnicianBalance;
use app\web\model\Project;
use app\web\model\OrderLogs;
use think\Db;


/**
 * 订单
 * @package
 */
class Order extends Base 
{
    public $status = ['待支付','待接单:info','待服务','待评价','已完成:success','已取消','已出发:danger','已到达:danger','开始服务:danger'];
    public function index()
    {
        $map = $this->getMap();
        foreach ($map as $key => $value) {
            if($value[0] == 'status') $map[$key][0] = 'a.status';
            if($value[0] == 'mobile') $map[$key][0] = 'a.mobile';
        }
        $data_list = myModel::alias('a')
                        ->where($map)
                        ->leftjoin('technician b','b.id=a.technician_id')
                        ->field('a.id,a.consignee,a.mobile,concat(a.province,a.city,a.area,a.address) as address,a.order_type,a.order_no,a.project_name as project_id,a.service_time,a.money,a.number,a.service_money,a.order_time,a.travel_price,a.tot_money,b.real_name as technician_id,a.create_time,a.pay_status,a.status,a.is_settlement_travel,a.technician_service')
                        ->order($this->getOrder('a.create_time desc'))
                        ->paginate()->each(function($item,$k){
                            $item['service_time'] /= 60;
                            if($item['status'] == 2){
                                if($item['technician_service'] == 1) $item['status'] = 6;
                                if($item['technician_service'] == 2) $item['status'] = 7;
                                if($item['technician_service'] == 3) $item['status'] = 8;
                            }
                        });
        return ZBuilder::make('table')
            ->setTableName('order')
            ->hideCheckBox()
            ->addColumns([
                // ['id','ID'],
                ['order_no','订单编号'],
                ['status','订单状态','status','',$this->status],
                ['order_type','订单类型','status','',['技师订单','商户订单']],
                ['consignee','用户姓名'],
                ['mobile','用户电话'],
                ['address','用户地址'],
                ['technician_id','预约技师'],
                ['project_id','预约项目'],
                ['order_time','预约时间'],
                ['service_time','服务时长(分钟)'],
                ['money','服务金额'],
                ['number','服务次数'],
                ['service_money','服务总金额'],
                ['travel_price','往返路费'],
                ['tot_money','订单总金额'],
                ['pay_status','支付状态','status','',['未支付','已支付']],
                ['create_time','提交时间'],
                ['right_button', '操作', 'btn']
            ])
            ->setRowList($data_list)
            ->setSearch(['project_name' => '项目名称','order_no'=>'订单号','mobile'=>'用户电话']) // 设置搜索参数
            ->addOrder('order_time,service_time')
            ->addTimeFilter('order_time','','预约开始时间,预约结束时间')
            ->addFilterList('technician_id',Technician::getSelectAll('check_status=1','real_name'))
            ->addFilterList('status',$this->status)
            ->addFilterList('order_type',['技师订单','商户订单'])
            ->addFilterList('project_id',Project::getSelectAll('check_status=1'))
            ->setColumnWidth('service_time,order_no',140)
            ->setColumnWidth('address',200)
            ->addRightButtons(['edit'=>['title'=>'查看']])
            ->addRightButtons(['settlementtravel'=>['title'=>'技师路费发放','href'=>url('settlementtravel',['id'=>'__id__'])]])
            ->replaceRightButton(['is_settlement_travel' => 1], '','settlementtravel') // 修改id为1的按钮
            ->fetch();
    }
    public function settlementtravel($id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if($data['is_refunds'] == 1){   //给技师结束路费
                $order = myModel::get($data['id']);
                $order->is_settlement_travel = 1;
                Db::startTrans();
                if(!$order->save()){
                    $this->error('操作失败');
                }
                $res = TechnicianBalance::change($order->technician_id,$order->refund_travel_price,6,'用户取消订单技师往返路费',$order->id);
                if(!$res){
                    Db::rollback();
                    $this->error('操作失败');
                }
                Db::commit();
            }
  
            $this->success('操作成功','index');
            
        }
        $info = myModel::alias('a')
                        ->where('a.id',$id)
                        ->leftjoin('technician b','b.id=a.technician_id')
                        ->field('a.id,a.order_type,a.order_no,b.nickName,a.refund_travel_price')
                        ->find()->toArray();
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['static:6', 'order_no','订单编号'],
                ['static:6','nickName','技师昵称'],
                ['static:6','refund_travel_price','用户取消订单技师往返路费'],
                ['radio','is_refunds','是否结算技师路费','',['否','是'],1]
            ])
            ->setFormData($info)
            ->fetch();
    }
    public function edit($id = null)
    {
        $info = myModel::alias('a')
                        ->where('a.id',$id)
                        ->leftjoin('technician b','b.id=a.technician_id')
                        ->leftjoin('shop c','c.id=a.shop_id')
                        ->field('a.id,a.consignee,a.mobile,concat(a.province,a.city,a.area,a.address) as address,a.order_type,a.order_no,a.project_name as project_id,a.service_time,a.money,a.number,a.service_money,a.order_time,a.travel_price,a.tot_money,b.real_name,b.shop_mobile as t_mobile,a.create_time,a.pay_status,a.pay_money+a.balance_money as pay_money,a.plus_money,a.coupon_money,a.integral_money,a.status,a.receiving_time,a.complete_time,c.shop_name,c.shop_mobile,a.is_refund,a.refund_desc')
                        ->find()->toArray();
        $info['service_time'] /= 60;
        $info['order_type1'] = ['技师订单','商户订单'][$info['order_type']];
        $info['pay_status1'] = ['未支付','已支付'][$info['pay_status']];
        $info['is_refund1'] = ['未退款','已退款'][$info['is_refund']];
        $info['status1'] = $this->status[$info['status']];
        $info['receiving_time'] = $info['receiving_time'] ? date('Y-m-d H:i',$info['receiving_time']) : '';
        $info['complete_time'] = $info['complete_time'] ? date('Y-m-d H:i',$info['complete_time']) : '';
        $order_logs = OrderLogs::where('order_id',$id)->select()->toArray();
        $content = "";
        foreach ($order_logs as $v) {

            $url = $v['latitude'] ? '<a target="_blank" href="https://apis.map.qq.com/tools/poimarker?type=0&marker=coord:'.$v['latitude'].",".$v['longitude'].';&key='.config('api.wx_map_key').'&referer=myapp">定位地址</a>' : '';
             $content .= "<tr>
                            <td>{$v['create_time']}</td>
                            <td>{$v['contents1']} ".$url."</td>
                        </tr>";
        }
        $html = <<<EOF
<div  id="product-table" style="display:none">
    <table class="table table-builder table-hover table-bordered table-striped ">
        <tr>
            <th>时间</th>
            <th>日记</th>
        </tr>
        {$content}
    </table>
</div>
<script>
    $(function(){
        $("#form_group_order_logs .form-control-static").html($("#product-table").html());

    })
</script>
EOF;
        return ZBuilder::make('form')
            ->addFormItems([
                ['hidden', 'id'],
                ['hidden','order_type'],
                ['hidden','status'],
                ['static:6', 'order_no','订单编号'],
                ['static:6', 'order_type1', '订单类型'],
                ['static:6', 'consignee','用户姓名'],
                ['static:6', 'mobile','用户电话'],
                ['static:6', 'address','用户地址'],
                ['static:6', 'order_time','预约时间'],
                ['static:6', 'real_name','预约技师'],
                ['static:6', 't_mobile','技师电话'],
                ['static:6', 'shop_name','商户名称'],
                ['static:6', 'shop_mobile','商户电话'],
                ['static:6', 'service_time', '服务时长(分钟)'],
                ['static:6', 'money', '服务金额'],
                ['static:6', 'number', '服务次数'],
                ['static:6', 'service_money','服务总金额'],
                ['static:6', 'travel_price','往返路费'],
                ['static:6', 'plus_money','加项/补差金额'],
                ['static:6', 'tot_money','订单总金额'],
                ['static:6', 'integral_money','积分抵扣金额'],
                ['static:6', 'coupon_money','优惠券抵扣金额'],
                ['static:6', 'pay_money','支付金额'],
                ['static:6', 'pay_status1','支付状态'],
                ['static:6', 'status1','订单状态'],
                ['static:6', 'receiving_time','接单时间'],
                ['static:6', 'complete_time','完成时间'],
                ['static:6', 'is_refund1','是否退款'],
                ['static:6', 'refund_desc','取消备注'],
                ['static', 'order_notes','订单备注'],
            ])
            ->addStatic('order_logs', '订单日记')
            ->setExtraHtml($html)
            ->hideBtn('submit')
            ->setFormData($info)
            ->setTrigger('order_type','1','shop_name,shop_mobile')
            ->setTrigger('status','2,3,4','receiving_time')
            ->setTrigger('status','4','complete_time')
            ->fetch();
    }
}